package tihi.com.horizotalscrolldemo;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by tihi on 2017/5/15.
 */

public class MyLinearLayout extends LinearLayout{

    private IMoveListener mListener;
    private int lastX = 0;
    private int lastY = 0;

    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMoveListener(IMoveListener listener) {
        mListener = listener;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) ev.getX();
                lastY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) (lastX - ev.getX());
                int dy = (int) (lastY - ev.getY());

                lastX = (int) ev.getX();
                lastY = (int) ev.getY();

                if (mListener != null) {
                    mListener.onMove(dx, dy);
                }
        }

        return super.dispatchTouchEvent(ev);
    }

    public interface IMoveListener{
        void onMove(int dx, int dy);
    }
}
